<?php 
require_once('mysql.php');
/* class Book
 *
 * description:
 * To init a book structure from mysql server
 * and do the basic insert delete .etc action. 
 * 
 * 
 * since version: 0.2
 */
class Book{
  /* var $name $ID $publisher $location
   *
   * type:string
   *
   * description:
   * to store the basic book information
   *
   *
   * since version:0.2
   */
  var $name;
  var $ID;
  var $publisher;
  var $location;
  /* var $is_available
   *
   * type:boolean
   *
   * description:
   * To identified if this book has been
   * checked out
   *
   * since version:0.2
   */
  var $is_available;
  /* var $username $checkout_time
   *
   * type:string
   *
   * description:
   * extra message given to tell whom is now 
   * reading this book if $is_available is false.
   * if $is_available is true, both var should be null.
   * 
   * since version:0.1
   */
  var $username;
  var $checkout_time;

  /**
   *  function: static function get_book_by_ID($ID)
   *
   *  description: inital a book instance by ID
   *
   *  param string $ID
   *
   *  return Book instance
   *
   *  @since version: 0.2 
   */
  static function get_book_by_ID($ID)
  {
    $con = MySQL::get_con();
    if(!MySQL::ckstr($ID))
      {
	return null;
      }
    $result = mysql_query("SELECT * FROM book WHERE ID = '$ID'",$con);
    if(!$result)
      {
	return null;
      }
    $row = mysql_fetch_array($result);
    if(!$row)
      {
	return null;
      }
    $book = new Book($row['ID'],
		     $row['name'],
		     $row['publisher'],
		     $row['location'],
		     $row['is_available'],
		     $row['username'],
		     $row['checkout_time']);
    if(!$book)
      {
	return null;
      }
    return $book;
  }
  
  /**
   *  function: static function checkout_history($ID)
   *
   *  description: return data to show the checkout_history of a
   *  certain book
   *
   *  param string $ID
   *
   *  return array(index=>(username,checkout_time,returned_time))
   *
   *  @since version: 0.2 
   */
  static function checkout_history($ID)
  {
    if(!MySQL::ckstr($ID))
      {
	return null;
      }
    $con = MySQL::get_con();
    if(!$con)
      {
	return null;
      }
    /* read checkout_record */
    $result = mysql_query("SELECT * from checkout_record WHERE ID = '$ID' and is_returned = true",$con);
    if(!$result)
      {
	return null;
      }
    $history = array();
    while($row = mysql_fetch_array($result))
      {
	$history[] = array('username'=>$row['username'],
			   'checkout_time'=>$row['checkout_time'],
			   'return_time'=>$row['return_time']);
      }
    return $history;
  }

  /**
   *  function: static function current_checkout($ID)
   *
   *  description: return data to show who currently using thisbook
   *
   *  param string $ID
   *
   *  return array(username,checkout_time)
   *
   *  @since version: 0.2 
   */
  static function current_checkout($ID)
  {
    if(!MySQL::ckstr($ID))
      {
	return null;
      }
    $con = MySQL::get_con();
    if(!$con)
      {
	return null;
      }
    /* read checkout_record */
    $result = mysql_query("SELECT * FROM checkout_record WHERE ID = '$ID' and is_returned = false",$con);
    if(!$result)
      {
	return null;
      }
    
    $row = mysql_fetch_array($result);
    if(!$row)
      {
	return null;
      }
    return array('username'=>$row['username'],'checkout_time'=>$row['checkout_time']);
  }
  
  /**
   *  function: static function search_books($keyword,$from=
   *  0,$to=0)
   *
   *  description: with the given keyword to find book in 
   *  database, and return it.$keyword will be searched in 
   *  every attribution of a book such as publisher bookname
   *  etc. if keyword = '' return all books;
   *
   *  param string $keyword
   *
   *  return array(book1,book2,...)
   *
   *  @since version: 0.2 
   */
  static function search_books($keyword,$from=0,$to=0)
  {
    $con = MySQL::get_con();
    if(!$con)
      {
	return null;
      }
    
    $result = mysql_query("SELECT * FROM book WHERE ID LIKE '%$keyword%' OR name LIKE '%$keyword%' OR publisher LIKE '%$keyword%' OR location LIKE '%$keyword%'",$con);
    if(!$result)
      {
	return null;
      }
    $books = array();
    $i = 1;
    
    while($row = mysql_fetch_array($result))
      {
	if(($from == 0&&$to <= $from)||($i-$from)*($i-$to)<=0)
	  {
	    $books[] = Book::get_book_by_ID($row['ID']);
	  } 
      }
    return $books;
  }
  
  /**
   *  function: static function search_books_num($keyword)
   *
   *  description: return how many books match the search keyword
   *
   *  param string $keyword
   *
   *  return int
   *
   *  @since version: 0.2 
   */
  static function search_books_num($keyword)
  {
    if(!MySQL::ckstr($keyword))
      {
	return false;
      }
    $con = MySQL::get_con();
    if(!$con)
      {
	return false;
      }
    $result = mysql_query("SELECT * FROM book WHERE ID LIKE '%$keyword%' OR name LIKE '%$keyword%' OR publisher LIKE '%$keyword%' OR location LIKE '%$keyword%'",$con);
    if(!$result)
      {
	return false;
      }
    return mysql_num_rows($result);
  }

  /**
   *  function: static checkout($ID)
   *
   *  description: checkout a book by given a ID
   *
   *  param string $ID $username
   *
   *  return boolean
   *
   *  @since version: 0.2 
   */
  static function checkout($ID)
  {
    $username = $_COOKIE['username'];
    /* check valid */
    
    if(!isset($username))
      {
	return false;
      }
    if(!MySQL::ckstr($username))
      {
	return false;
      }
    
    if(!MySQL::ckstr($ID))
      {
	return false;
      }
    $con = MySQL::get_con();
    if(!$con)
      {
	return false;
      }
    
    /* ready to write database */
    /* update book */
    $result = mysql_query("SELECT * FROM book WHERE ID = '$ID'and is_available = true",$con);
    if(mysql_num_rows($result)==0)
      return false;
    $row = mysql_fetch_array($result);
    
    $result = mysql_query("UPDATE book SET is_available = false,username = '$username',checkout_time = null WHERE ID = '$ID' and is_available = true",$con);
    $name = $row['name'];
   
    /* write checkout_record */
    $result = mysql_query("INSERT INTO checkout_record SET checkout_time = null,username = '$username', name = '$name' ,is_returned = false,ID = '$ID'",$con);
    if(!$result)
      {
	return false;
      }
    return true;
  }
  
  /**
   *  function: static function return_book($ID)
   *
   *  description: return a book by give a ID
   *
   *  param string $ID
   *
   *  return boolean
   *
   *  @since version: 0.2 
   */
  static function return_book($ID)
  {
    $username = $_COOKIE['username'];
    if(!isset($username))
      {
	return false;
      }
    $con = MySQL::get_con();
    if(!$con)
      {
	return false;
      }
    if(!MySQL::ckstr($ID))
      {
	return false;
      }
    if(!MySQL::ckstr($username))
      {
	return false;
      }
    /* write book */
    $result = mysql_query("SELECT * FROM book WHERE ID='$ID' and is_available = false and username = '$username'",$con);
    if(!$result||mysql_num_rows($result)==0)
      {
	return false;
      }
    //$row = mysql_fetch_array($result);
    $result = mysql_query("UPDATE book SET is_available = true , username = '',checkout_time = 0 WHERE ID = '$ID' and is_available = false and username = '$username'",$con);
    /* write checkout_record */
    $result = mysql_query("UPDATE checkout_record SET is_returned = true ,return_time = null WHERE ID = '$ID' and is_returned = false",$con);
    if(!$result)
      {
	return false;
      }
    return true;    
  }
  /**
   *  function: static function del_book_by_ID($ID)
   *
   *  description: like it's name
   *
   *  param string $ID
   *
   *  return boolean
   *
   *  @since version: 0.2 
   */
  static function del_book_by_ID($ID)
  {
    if(!MySQL::ckstr($ID))
      {
	return false;
      }
    $con = MySQL::get_con();
    if(!$con)
      {
	return false;
      }
    if(!mysql_query("DELETE FROM book WHERE ID = '$ID'")||mysql_affected_rows()!=1)
      {
	return false;
      }
    return true;
  }

  /**
   *  function: function save_this()
   *
   *  description: save this book instance to mysql
   *
   *  param void
   *
   *  return boolean
   *
   *  @since version: 0.2 
   */
  function save_this()
  {
    if(!$this->is_valid())
      {
	return false;
      }
    $con = MySQL::get_con();
    if(!$con)
      {
	return false;
      }
    if(!mysql_query("INSERT INTO book SET ID = '$this->ID',name = '$this->name', publisher = '$this->publisher', location = '$this->location',is_available = $this->is_available",$con))
      {
	return false;
      }
    return true;
  }
  /**
   *  function: function is_valid
   *
   *  description: to check if this instance is a valid bood
   *
   *  param void 
   *
   *  return boolean
   *
   *  @since version: 0.2 
   */
  
  function is_valid()
  {
    if(!isset($this->name,$this->ID,$this->publisher,$this->location))
      {
	return false;
      }
    if(!MySQL::ckstr($this->name)||
       !MySQL::ckstr($this->ID)||
       !MySQL::ckstr($this->publisher)||
       !MySQL::ckstr($this->location))
      {
	return false;
      }
    return true;
  }
  
  /**
   *  function: function update_this()
   *
   *  description: like it's name
   *
   *  param void
   *
   *  return boolean
   *
   *  @since version: 0.2 
   */
  function update_this()
  {
    if(!$this->is_valid())
      {
	return false;
      }
    $con = MySQL::get_con();
    if(!$con)
      {
	return false;
      }
    $result = mysql_query("UPDATE book SET ID='$this->ID',name = '$this->name' , publisher = '$this->publisher' , location = '$this->location' WHERE ID = '$this->ID'",$con);
    if(!$result)
      {
	return false;
      }
    if (mysql_affected_rows()==0)
      {
	return false;
      }
    return true;
  }
  
  
  
  
  /**
   *  function: function Book
   *
   *  description: default Constructor of the Book class
   *
   *  param string $ID,$name,$publisher,$location 
   *        boolean is_available
   *        string username checkout_time
   *
   *  return void
   *
   *  @since version: 0.2 
   */
  function Book($ID,$name,$publisher,$location,$is_available=true,$username="",$checkout_time="")
  {
    $this->ID = $ID;
    $this->name = $name;
    $this->publisher = $publisher;
    $this->location = $location;
    $this->is_available = $is_available;
    $this->username = $username;
    $this->checkout_time = $checkout_time;
  }
}


?>